package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.tingshu.vo.account.AccountDeductVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"all"})
public class UserAccountApiController {

	@Autowired
	private UserAccountService userAccountService;

	@TsLogin
	@Operation(summary = "获取账户可用金额")
	@GetMapping("getAvailableAmount")
	public Result getAvailableAmount(){
		Long userId = AuthContextHolder.getUserId();
		UserAccount userAccount = userAccountService.getOne(new LambdaQueryWrapper<UserAccount>().eq(UserAccount::getUserId,userId));
		return Result.ok(userAccount.getAvailableAmount());

	}

	/**
	 * 检查及扣减账户余额
	 * @param accountDeductVo
	 * @return
	 */
	@Operation(summary = "检查及扣减账户余额")
	@PostMapping("/checkAndDeduct")
	public Result checkAndDeduct(@RequestBody AccountDeductVo accountDeductVo){
		// 调用服务此方法.
		Boolean result = userAccountService.checkAndDeduct(accountDeductVo);
		// 返回数据
		return result?Result.ok():Result.fail();
	}
}

